package com.example.kaoshi;

import java.util.HashMap;
import java.util.Scanner;

/**
 * Created by Quincy on 2018/9/28.
 */
public class LRUCache {

    int capacity;
    HashMap<Integer, Node> map = new HashMap<Integer, Node>();
    Node head=null;//先声明一个头结点和一个尾节点
    Node end=null;
    public LRUCache(int capacity) {
        this.capacity = capacity;
    }

    public int get(int key){
        if (map.containsKey(key)){
            Node n = map.get(key);
            remove(n);
            setHead(n);
            return n.value;
        }
        return -1;
    }

    private void remove(Node n) {
        if (n.pre!=null){
            n.pre.next = n.next;
        }else {
            head = n.next;
        }
        if (n.next != null)
            n.next.pre = n.pre;
        else
            end = n.pre;
    }

    public void setHead(Node n){
        n.next = head;
        n.pre = null;
        if(head!=null)
            head.pre = n;

        head = n;
        if (end == null)
            end = head;
    }

    public void set(int key, int value){
        if (map.containsKey(key)){
            Node old = map.get(key);
            old.value = value;
            remove(old);
            setHead(old);
        }else {
            Node creat = new Node(key, value);
            if (map.size() >= capacity){
                map.remove(end.key);
                remove(end);
                setHead(creat);
            }else {
                setHead(creat);
            }
            map.put(key, creat);
        }
    }

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n  = scanner.nextInt();
        LRUCache lruCache = new LRUCache(n);
        while (scanner.hasNext()){

        }
    }
}
